"""
对requests模块的二次封装
提供一个统一的接口调用的方法
使用requests模块的session机制
在模块内部处理baseUrl，在测试用例使用该模块提供的方法时就只需要考虑path
"""
import requests
from lib.config import config
from lib.logger import Logger

class HTTPClient(object):
    def __init__(self):
        self.client = requests.Session()
        self.logger = Logger()

    def __del__(self):
        self.client.close()

    def request(self, path, **kwargs):
        baseUrl = config['server']['protocol'] + "://" + config['server']['ip'] + ":" + config['server']['port']
        url = baseUrl + path
        r = self.client.request(url = url, **kwargs)
        self.logger.debug(r.request.url)
        self.logger.debug(r.request.headers)
        self.logger.debug(r.request.body)
        self.logger.debug(r.status_code)
        self.logger.debug(r.headers)
        self.logger.debug(r.text)
        return r

    def set_headers(self, headers: dict):
        self.client.headers.update(headers)




